package com.xd.cloud.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xd.cloud.common.Constants;
import com.xd.cloud.common.bean.MessageBean;
import com.xd.cloud.entity.core.Dic;
import com.xd.cloud.entity.device.Device;
import com.xd.cloud.service.core.DicService;
import com.xd.cloud.service.device.DeviceService;

/**   
 * 功能描述：设备信息管理controller
 * @author: wangshanshan   
 * @date: 2017年12月11日 上午10:30:28 
 */
@Controller
@RequestMapping("/device")
public class DeviceController extends BaseController{

	@Autowired
	private DeviceService deviceService;
	@Autowired
	private DicService disService;
	/**
	 * 根据条件搜索信息列表
	 * @return Object
	 */
	@RequestMapping(value = "/searchList")
	@ResponseBody
	public Object searchList(HttpServletRequest request, HttpServletResponse response, String page, String limit) {
		try {
		    int pageNo = 0;
		    int pageSize = 0;
		    if(StringUtils.isNotEmpty(page)) {
		    	pageNo = Integer.parseInt(page);
		    }
		    if(StringUtils.isNotEmpty(limit)) {
		    	pageSize = Integer.parseInt(limit);
		    }
		    List<Device> getList = deviceService.getAllDevice(pageNo, pageSize);
			if(getList!=null && getList.size()>0) {
				Map<Object, Object> info = new HashMap<Object, Object>();  
			    info.put("pageData", getList);  
			    info.put("total", getList.size()); 
				return info;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	/**
	 * 查看信息详情
	 * @return Object
	 */
	@RequestMapping(value = "/showDetail")
	@ResponseBody
	public Object showDetail(HttpServletRequest request, HttpServletResponse response,String id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			Device item = new Device();
			if(StringUtils.isNotEmpty(id) && !id.equals("-1")) {
				item = deviceService.getDeviceObjectById(Integer.valueOf(id));
			}
			List<Dic> factoryTypeList = disService.getDisByType(Constants.FACTORY_TYPE);
			List<Dic> deviceTypeList = disService.getDisByType(Constants.DEVICE_TYPE);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("item", item);
			map.put("factoryType", factoryTypeList);
			map.put("deviceType", deviceTypeList);
			msgBean.setSuccess(true);
			msgBean.setMsg(map);
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	/**
	 * 添加or修改数据
	 * @return Object
	 */
	@RequestMapping(value = "/saveOrUpdateInfo")
	@ResponseBody
	public Object saveOrUpdateInfo(HttpServletRequest request, HttpServletResponse response,String id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			Device item = new Device();
			if(StringUtils.isNotEmpty(id)) {
			    String company_id = request.getParameter("company_id");
			    String sim_id = request.getParameter("sim_id");
			    String old_sim_id = request.getParameter("old_sim_id");
			    Integer simId = null;
			    if(StringUtils.isNotEmpty(sim_id)) {
			    	simId = Integer.valueOf(sim_id);
			    }
				item.setDevice_id(request.getParameter("device_id"));
				item.setSerial_id(request.getParameter("serial_id"));
				item.setCompany_id(Integer.valueOf(company_id));
				item.setDevice_type(Integer.valueOf(request.getParameter("device_type")==null?"0":request.getParameter("device_type")));
				item.setFactory_type(Integer.valueOf(request.getParameter("factory_type")==null?"0":request.getParameter("factory_type")));
				item.setBrand(request.getParameter("brand"));
				item.setModel(request.getParameter("model"));
				item.setSoftware_ver(request.getParameter("software_ver"));
				item.setHardware_ver(request.getParameter("hardware_ver"));
				item.setProduct_manufacturer(request.getParameter("product_manufacturer"));
				item.setRemark(request.getParameter("remark"));
				item.setUpdate_time(new Date());
				if(id.equals("-1")) {//添加操作
					Device temp = deviceService.getDeviceByDeviceId(item.getDevice_id());
					if(temp!=null) {//设备号存在，不允许添加
						msgBean.setMsg("设备号已存在！");
						return msgBean;
					}
					item.setCreate_time(new Date());
					deviceService.save(item);
				}else {//修改操作
					Device temp = deviceService.getDeviceById(Integer.valueOf(id));
					item.setId(Integer.valueOf(id));
					item.setInstall(temp.getInstall());
					item.setInstall_time(temp.getInstall_time());
					item.setCreate_time(temp.getCreate_time());
					deviceService.update(item);
					if(StringUtils.isNotEmpty(old_sim_id)) {
				    	int oldSimId = Integer.valueOf(old_sim_id);
				    	deviceService.deleteDeviceSim(temp.getId(), oldSimId);
				    }
				}
				Device temp = deviceService.getDeviceByDeviceId(item.getDevice_id());
				if(temp!=null && simId!=null) {
					deviceService.saveDeviceSim(temp.getId(),simId);
				}
				msgBean.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	/**
	 * 删除操作
	 * @return Object
	 */
	@RequestMapping(value = "/deleteInfo")
	@ResponseBody
	public Object deleteInfo(HttpServletRequest request,String id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(id)) {
				boolean flag = deviceService.deleteInfo(Integer.valueOf(id));
				msgBean.setSuccess(flag);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return msgBean;
	}
	
	/**
	 * 根据安装情况，获取所有设备列表，没有分页，方法基本是为了所有没有安装的设备使用
	 * @return Object
	 */
	@RequestMapping(value = "/getDevice")
	@ResponseBody
	public Object getDevice(HttpServletRequest request,int install) {
		List<Device> list = deviceService.getDeviceList(0);
		return new MessageBean(true,"",list);
	}
}
